package xcmg.device.service.announcement;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.announcement.BasicCommentDO;
import xcmg.device.dao.mapper.CompanyDealerMapper;
import xcmg.device.dao.mapper.announcement.BasicCommentMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.announcement.BasicCommentVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.List;

@Service
public class BasicCommentService {

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private CompanyDealerMapper companyDealerMapper;

    @Autowired
    private BasicCommentMapper basicCommentMapper;

    /**
     * 评论信息查询
     * @param condition
     * @return
     */
    public PageCommonVO list(SearchCommonVO<BasicCommentVO> condition) {
        PageCommonVO result = new PageCommonVO();
        //按照px 排序
        PageHelperPlus.orderBy("px desc");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<BasicCommentDO> doList = basicCommentMapper.list(condition.getFilters());
        List<BasicCommentVO> voList = new ArrayList<>();
        for (BasicCommentDO entity : doList) {
            BasicCommentVO model = new BasicCommentVO();
            BeanUtils.copyProperties(entity, model);
            voList.add(model);
        }
        result.setPageInfo(new PageInfo(doList));
        result.setPageInfoList(voList);
        return result;
    }

    /**
     * 新增评论信息
     * @param model
     * @return
     */
    public ErrorCode add(BasicCommentVO model){
        if(null== model.getAnnouncementId() || StringUtils.isEmpty(model.getAnnouncementId())){
            return ErrorCode.AnnouncementIdIsEmpty;
        }
        if(null== model.getContent() || StringUtils.isEmpty(model.getContent())){
            return ErrorCode.CommentContentIsEmpty;
        }
        model.setId(fastGenClient.textGuid().getValue());
        model.setPx(basicCommentMapper.countPxByAnnouncementId(model.getAnnouncementId()));
        if(basicCommentMapper.insert(model)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }

    /**
     * 更新评论信息
     * @param model
     * @return
     */
    public ErrorCode update(BasicCommentVO model){
        if(null== model.getAnnouncementId() || StringUtils.isEmpty(model.getAnnouncementId())){
            return ErrorCode.AnnouncementIdIsEmpty;
        }
        if(null== model.getContent() || StringUtils.isEmpty(model.getContent())){
            return ErrorCode.CommentContentIsEmpty;
        }
        if(basicCommentMapper.update(model)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }

    /**
     * 单条评论信息查询
     * @param id
     * @return
     */
    public BasicCommentVO item(String id){
        BasicCommentDO entity=basicCommentMapper.selectByPrimaryKey(id);
        if(entity==null){
            return null;
        }
        BasicCommentVO model=new BasicCommentVO();
        BeanUtils.copyProperties(entity,model);
        return model;
    }

    /**
     * 删除评论信息
     * 支持多个
     * @param ids
     * @return
     */
    public ErrorCode remove(List<String> ids){
        if(basicCommentMapper.updateDel(ids)==0){
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }
}
